package mysql

import (
	"gorm.io/gorm"
	"novel-sys/goods/goods"
)

type Books struct {
	gorm.Model
	UserId  int64 `gorm:"type:int(11)"`
	NovelId int64 `gorm:"type:int(11)"`
}

func (b *Books) BooksAdd(db *gorm.DB) error {
	return db.Debug().Create(b).Error
}

func (b *Books) BooksById(db *gorm.DB, in *goods.BooksAddRequest) error {
	return db.Where("user_id=?", in.UserId).Where("novel_id=?", in.NovelId).Find(b).Error
}

func (b *Books) BookDel(db *gorm.DB, in *goods.BooksDelRequest) error {
	return db.Where("user_id=?", in.UserId).Where("novel_id=?", in.NovelId).Delete(b).Error
}
func BooksList(db *gorm.DB, in *goods.BooksListRequest) []*goods.BookList {
	var data []*goods.BookList
	db.Debug().Model(&Books{}).
		Select(
			"novels.*",
			"users.head_image",
			"users.nick_name").
		Joins("LEFT JOIN novels ON novels.user_id=books.id").
		Joins("LEFT JOIN users ON novels.user_id=users.id").
		Where("books.user_id=?", in.UserId).
		Find(&data)
	return data
}
